<template>
  <div id="reference_file" class="container">
    <div class="content">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item
          class="title"
          :to="{ path: '/system_set/course_library' }"
          v-if="toPageObject.isFrom == '1'"
        >课程库管理</el-breadcrumb-item>
        <el-breadcrumb-item
          class="title"
          :to="{ path: '/system_set/curriculum_setting' }"
          v-else
        >课程管理</el-breadcrumb-item>
        <el-breadcrumb-item
          class="title"
          v-if="toPageObject.isFrom == '1'"
          :to="{ path: '/system_set/teacher_syllabus_library',query:toPageObject }"
        >{{toPageObject.title}}</el-breadcrumb-item>
        <el-breadcrumb-item
          class="title"
          v-else
          :to="{ path: '/system_set/teacher_syllabus_course',query:toPageObject }"
        >{{toPageObject.title}}</el-breadcrumb-item>
        <el-breadcrumb-item class="title">{{title}}(教参)</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="panel">
        <div class="condition">
          <div class="row_one">
            <div class="left_btn" @click="openAdd(false)">新增课节资源</div>
            <div class="right_btn" @click="openAdd(true)">新&nbsp&nbsp增</div>
          </div>
        </div>
        <div>
          <table class="table" cellspacing="0">
            <thead class="thead">
              <tr>
                <td style="width: 6rem;">资源名称</td>
                <td style="width: 3rem;">资源状态</td>
                <td style="width: 4rem;">资源类型</td>
                <td style="width: 3rem;">添加时间</td>
                <td style="width: 3rem;">添加人</td>
                <td style="width: 3rem;">操作</td>
              </tr>
            </thead>
            <tbody class="tbody">
              <tr v-for="item,index in resourceList">
                <td v-if="item.isResEdit">
                  <el-input
                    placeholder="请输入资源名称"
                    v-model="item.resourceName"
                    @blur.prevent="itemEdit(item)"
                  >
                    <i
                      slot="suffix"
                      class="el-input__icon el-icon-edit"
                      style="cursor: pointer;"
                      @click="itemEdit(item)"
                    ></i>
                  </el-input>
                </td>
                <td
                  v-else
                  style="max-width: 6rem;word-wrap: break-word;cursor: pointer;"
                  @click="clickItem(item)"
                >
                  {{item.resourceFileName != '.url'?item.resourceName:item.bosResourceUrl}}
                  <i
                    v-if="item.processState == 2 && item.resourceFileName != '.url'"
                    class="el-icon-edit nameEdit"
                    @click.stop="openEdit(item)"
                  ></i>
                </td>
                <td>{{item.resourceFileName}}</td>
                <td>{{item.processStateName}}</td>
                <td>{{item.createTime}}</td>
                <td>{{item.createUserName}}</td>
                <td>
                  <div class="edit_btn">
                    <div
                      class="btn_img_delete"
                      v-if="item.processState != 3"
                      @click="resourceDelete(item)"
                    >删除</div>
                    <div
                      class="btn_img_reupload"
                      v-if="item.processState == 3"
                      @click="resourceDelete(item)"
                    >重新上传</div>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <el-pagination
          class="page"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="tabledisplayObj.currPage"
          @size-change="pageSizeChange"
          @current-change="tabledisplayChangePage"
          :total="tabledisplayObj.total"
        ></el-pagination>
      </div>
    </div>
    <resourceImport ref="resourceImport" @callback="closeAdd"></resourceImport>
    <talkcloudImport ref="talkcloudImport" @callback="closeAdd"></talkcloudImport>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from "vuex"; //先要引入
import resourceImport from "../internal_manage/common/resource_import";
import talkcloudImport from "../internal_manage/common/talkcloud_import";
let vm;
let user;
export default {
  data() {
    return {
      resourceList: [],
      title: "",
      syllId: "",
      syllBosUrl: "",
      isAddRefer: true,
      bosBaseUrl: "",
      courseTypeId: "",
      tabledisplayObj: {
        currPage: 1,
        pageSize: 10,
        total: 0,
        resourceParentId: 0
      },
      toPageObject: {
        libraryCourseId: "",
        courseTypeId: "",
        title: "",
        isFrom: "",
        currPage: ""
      }
    };
  },
  components: {
    resourceImport,
    talkcloudImport
  },
  methods: {
    /**编辑名称 */
    openEdit: function(item) {
      item.isResEdit = true;
      vm.$nextTick(() => {
        $("#" + item.resourceId).focus();
      });
    },
    clickItem(item) {
      if(item.talkcloudFileId != null){
         vm.$message({
              message: "在线课程资源暂不能预览！",
              type: "warning"
            });
            return;
      }
      if (!vm.$store.state.permit["999"]) {
          tool.message({
            showClose: true,
            message: "当前用户缺少权限",
            type: "warning"
          });
          return;
        }
        var path = vm.$store.state.permit["999"].path;
        let routeData = this.$router.resolve({
          path: path,
          query: { resourceId: item.resourceId,index:2 }
        });
        window.open(routeData.href);

    },
    /**
     * @param {Object} item 数据
     */
    resourceDelete(item) {
      this.$confirm("此操作将删除该资源文件, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let paras = {
            resourceId: item.resourceId
          };
          api.resourceDelete(paras).then(function(data) {
            if (data.resultCode == 2000) {
              tool.message({
                showClose: true,
                message: "删除成功",
                type: "success"
              });
              vm.initData();
            } else {
              vm.$message.error(data.message);
            }
          });
        })
        .catch(() => {});
    },
    itemEdit(item) {
      let paras = {
        resourceParentId: vm.tabledisplayObj.resourceParentId,
        resourceId: item.resourceId,
        resourceName: item.resourceName + item.resourceFileName
      };
      api.resourceUpdate(paras).then(function(data) {
        if (data.resultCode == 2000) {
          tool.message({
            showClose: true,
            message: "编辑成功",
            type: "success"
          });
          item.isResEdit = false;
          // vm.initData();
        } else {
          vm.$message.error(data.message);
        }
      });
    },
    pageSizeChange: function(val) {
      //页条数发生改变
      vm.tabledisplayObj.pageSize = val;
      this.initData();
    },
    /**表格显示形式翻页 */
    tabledisplayChangePage(val) {
      vm.tabledisplayObj.currPage = val;
      this.initData();
    },
    /**
     * 初始化数据
     */
    initData() {
      // debugger;
      api.resourceList(vm.tabledisplayObj).then(function(data) {
        if (data.resultCode == 2000) {
          data.list.forEach(item => {
            item.createTime = item.createTime.split(" ")[0];
            item.isResEdit = false;
            let len = item.resourceName.lastIndexOf(".");
            item.resourceFileName = item.resourceName.substring(len); //文件类型
            item.resourceName = item.resourceName.substring(0, len); //文件名
            if (item.processState == 1) {
              item.processStateName = "处理中";
            } else if (item.processState == 3) {
              item.processStateName = "处理失败";
            } else {
              item.processStateName = "处理成功";
            }
          });
          vm.resourceList = data.list;
          vm.tabledisplayObj.total = data.totalCount;
        } else {
          vm.$message.error(data.message);
        }
      });
    },
    /**
     * 向数组中添加数据
     */
    openAdd(isAddRefer) {
      vm.isAddRefer = isAddRefer;
      if (isAddRefer) {
        if (vm.courseTypeId == 5) {
          vm.$refs.talkcloudImport.init({
            resourceParentId: vm.tabledisplayObj.resourceParentId
          });
        } else {
          vm.$refs.resourceImport.init({
            bosBaseUrl: vm.bosBaseUrl,
            resourceParentId: vm.tabledisplayObj.resourceParentId,
            isFrom: vm.toPageObject.isFrom
          });
        }
      } else {
        if (vm.courseTypeId == 5) {
          vm.$refs.talkcloudImport.init({
            resourceParentId: vm.syllId
          });
        } else {
          vm.$refs.resourceImport.init({
            bosBaseUrl: vm.syllBosUrl,
            resourceParentId: vm.syllId,
            isFrom: vm.toPageObject.isFrom
          });
        }
      }
    },
    /**
     * 增加返回函数
     */
    closeAdd() {
      if (vm.isAddRefer) {
        this.initData();
      } else {
        let queryObj = {
          courseTypeId: vm.courseTypeId,
          title: vm.toPageObject.title,
          subTitle: vm.title,
          currPage: vm.toPageObject.currPage,
          syllId: vm.syllId,
          referId: vm.tabledisplayObj.resourceParentId,
          syllBosUrl: vm.syllBosUrl,
          referBosUrl: vm.bosBaseUrl,
          isFromCourse: vm.toPageObject.isFrom,
          r: vm.toPageObject.libraryCourseId
        };
        var path = vm.$store.state.permit["57"].path;
        //传递参数
        vm.$router.push({
          path: path,
          query: queryObj
        });
      }
    }
  },
  mounted() {
    vm = this;
    user = tool.getItem(lsKey.user, true); //得到当前用户信息
    let resourceParentId = this.$route.query.referId;
    vm.tabledisplayObj.resourceParentId = resourceParentId;
    vm.title = this.$route.query.subTitle;
    vm.bosBaseUrl = this.$route.query.referBosUrl;
    vm.courseTypeId = this.$route.query.courseTypeId;

    vm.syllId = this.$route.query.syllId;
    vm.syllBosUrl = this.$route.query.syllBosUrl;
    vm.toPageObject.libraryCourseId = this.$route.query.r;
    vm.toPageObject.courseTypeId = vm.courseTypeId;
    vm.toPageObject.title = this.$route.query.title;
    vm.toPageObject.isFrom = this.$route.query.isFromCourse;
    vm.toPageObject.currPage = this.$route.query.currPage;
    let isUpload = this.$route.query.isUpload;
    console.log(isUpload);
    if (isUpload && isUpload == "1") {
      if (vm.courseTypeId == 5) {
        vm.$refs.talkcloudImport.init({
          resourceParentId: vm.tabledisplayObj.resourceParentId
        });
      } else {
        vm.$refs.resourceImport.init({
          bosBaseUrl: vm.bosBaseUrl,
          resourceParentId: vm.tabledisplayObj.resourceParentId,
          isFrom: vm.toPageObject.isFrom
        });
      }
    }
    vm.initData();
  },
  computed: {}
};
</script>

<style lang="scss">
#reference_file {
  .left_btn {
    background-image: url(../../assets/img/clue_manage/add_white.png);
    width: 1.76rem;
    height: 0.38rem;
    line-height: 0.38rem;
    float: left;
    font-size: 0.16rem;
    font-weight: bold;
    background-color: #3885f8;
    border-radius: 0.1rem;
    box-shadow: 0 0 0.2rem 0 rgba(56, 133, 248, 0.2);
    padding-left: 0.5rem;
    cursor: pointer;
    background-repeat: no-repeat;
    color: #ffffff;
    background-size: 0.18rem;
    background-position: 0.2rem center;
  }
  .el-input__inner {
    border-radius: 0.08rem;
    font-size: 0.16rem;
    color: #666666;
    height: 0.36rem;
    line-height: 0.36rem;
  }

  .el-input__icon {
    line-height: 0.36rem;
  }

  .input_search {
    .el-input__inner {
      background-color: rgb(247, 248, 250);
    }
  }

  .syllabusRes {
    text-decoration: underline;
    color: #3885f8;
    font-size: 0.16rem;
    cursor: pointer;
  }

  .condition {
    padding-bottom: 0rem;
  }

  .el-breadcrumb__separator {
    color: #999999;
  }

  .el-breadcrumb__separator {
    color: #999999;
  }
  .el-breadcrumb__inner.is-link {
    font-weight: 400;
    color: #333333;
  }
  .el-breadcrumb__inner.is-link:hover {
    color: #3885f8;
  }
  .el-breadcrumb__item:last-child .el-breadcrumb__inner {
    color: #3885f8;
  }
  .back_Btn {
    color: #3885f8;
    // line-height: 0.5rem;
    float: left;
    padding-left: 0.34rem;
    background-image: url("../../assets/img/education_manage/orderForeignTeacher/back.png");
    background-repeat: no-repeat;
    font-size: 0.18rem;
    background-size: 0.2rem;
    background-position: 0.1rem center;
    cursor: pointer;
  }

  .edit_btn {
    width: 100%;
    display: flex;
    justify-content: center;
    align-content: center;
    text-align: center;

    .btn_img_delete {
      color: #3885f8;
      padding-left: 0.28rem;
      background-image: url("../../assets/img/internal_manage/delete.png");
      background-repeat: no-repeat;
      background-size: 0.14rem;
      background-position: 0.09rem center;
      cursor: pointer;
    }
    .btn_img_reupload {
      color: #3885f8;
      padding-left: 0.28rem;
      margin-left: 0.2rem;
      background-image: url("../../assets/img/internal_manage/reupload.png");
      background-repeat: no-repeat;
      background-size: 0.14rem;
      background-position: 0.09rem center;
      cursor: pointer;
    }
  }
}
</style>
<style scoped src="@/style/module/table.css"></style>
<style scoped src="@/style/clue_manage/clue_manage.css"></style>
<style scoped>
.right_btn {
  background-image: url(../../assets/img/clue_manage/add_white.png);
  width: 1.54rem;
}
</style>
